package com.trucle.vntraffic;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
/*
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
*/

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class VnTraffic extends MapActivity {

	private MapController mapController;
	private MapView mapView;
	private LocationManager locationManager;
	private OverlaysIcon itemizedoverlay;
	private MyLocationOverlay myLocationOverlay;
	
	/*
	private static String url = "http://q.esolvn.com/traffic/?action=mobile&java=1";
	private static final String TAG_TITLE = "title";
	private static final String TAG_LAT = "lat";
	private static final String TAG_LONG = "long";
	private static final String TAG_DISTRICT = "folder_id";
	private static final String TAG_STATUS = "last_value";
	*/
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vn_traffic); // bind the layout to the activity
                
        // Configure the Map
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        //mapView.setSatellite(true);
        mapController = mapView.getController();
        mapController.setZoom(18); // Zoon 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());

        myLocationOverlay = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(myLocationOverlay);

        myLocationOverlay.runOnFirstFix(new Runnable() 
        {
          public void run() {
            mapView.getController().animateTo(myLocationOverlay.getMyLocation());
          }
        });

        Drawable drawable = this.getResources().getDrawable(R.drawable.busy);
        itemizedoverlay = new OverlaysIcon(this, drawable);
        createMarker();
    }
    
	@Override
	  protected boolean isRouteDisplayed() {
	    return false;
	  }

	  public class GeoUpdateHandler implements LocationListener {

	    //@Override
	    public void onLocationChanged(Location location) {
	      int lat = (int) (location.getLatitude() * 1E6);
	      int lng = (int) (location.getLongitude() * 1E6);
	      GeoPoint point = new GeoPoint(lat, lng);
	      createMarker();
	      mapController.animateTo(point); // mapController.setCenter(point);

	    }

	    //@Override
	    public void onProviderDisabled(String provider) {
	    }

	    //@Override
	    public void onProviderEnabled(String provider) {
	    }

	    //@Override
	    public void onStatusChanged(String provider, int status, Bundle extras) {
	    }
	  }

	  private void createMarker() {
	    GeoPoint p = mapView.getMapCenter();
	    GeoPoint gp = new GeoPoint((int)(10.777616 * 1E6), (int)(106.681680 * 1E6));
	    GeoPoint g1 = new GeoPoint((int)(10.798630 * 1E6), (int)(106.726266 * 1E6));
	    GeoPoint g2 = new GeoPoint((int)(10.801464 * 1E6), (int)(106.711898 * 1E6));
	    GeoPoint g3 = new GeoPoint((int)(10.771381 * 1E6), (int)(106.693232 * 1E6));
	    GeoPoint g4 = new GeoPoint((int)(10.656525 * 1E6), (int)(106.787821 * 1E6));
	    GeoPoint g5 = new GeoPoint((int)(10.763726 * 1E6), (int)(106.659939 * 1E6));
	    
	    OverlayItem overlayitem = new OverlayItem(p, "", "");
	    OverlayItem overlayitem1 = new OverlayItem(gp, "", "");
	    OverlayItem overlayitem2 = new OverlayItem(g1, "", "");
	    OverlayItem overlayitem3 = new OverlayItem(g2, "", "");
	    OverlayItem overlayitem4 = new OverlayItem(g3, "", "");
	    OverlayItem overlayitem5 = new OverlayItem(g4, "", "");
	    OverlayItem overlayitem6 = new OverlayItem(g5, "", "");
	    
	    itemizedoverlay.addOverlay(overlayitem);
	    itemizedoverlay.addOverlay(overlayitem1);
	    itemizedoverlay.addOverlay(overlayitem2);
	    itemizedoverlay.addOverlay(overlayitem3);
	    itemizedoverlay.addOverlay(overlayitem4);
	    itemizedoverlay.addOverlay(overlayitem5);
	    itemizedoverlay.addOverlay(overlayitem6);
	    
	    if (itemizedoverlay.size() > 0) {
	      mapView.getOverlays().add(itemizedoverlay);
	    }
	  }

	  @Override
	  protected void onResume() {
	    super.onResume();
	    myLocationOverlay.enableMyLocation();
	    myLocationOverlay.enableCompass();
	  }

	  @Override
	  protected void onPause() {
	    super.onPause();
	    myLocationOverlay.disableMyLocation();
	    myLocationOverlay.disableCompass();
	  }
}
